package search;

public class Solution {
    public int search(int[] nums, int target) {
        return searchRight(nums,target) - searchLeft(nums,target) + 1;
    }
    // 找最左边的值
    private int searchLeft(int[] nums,int target) {
        int l = 0,r = nums.length-1;
        while (l<=r) {
            int mid = (l+r)/2;
            if(nums[mid] < target) {
                l = mid + 1;
            }else {
                r = mid - 1;
            }
        }
        return l;
    }
    // 找最右边的值
    private int searchRight(int[] nums,int target) {
        int l = 0,r = nums.length-1;
        while (l<=r) {
            int mid = (l+r)/2;
            if(nums[mid] > target) {
                r = mid-1;
            }else {
                l = mid+1;
            }
        }
        return r;
    }
    // 二分法找数组最左边的target的位置和最右边的target的位置
}
